package com.ucar.training.controller;

import com.ucar.training.common.Result;
import com.ucar.training.domain.User;
import com.ucar.training.service.IUserService;
import com.ucar.training.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author XiongMM
 * @date 2019/7/30 10:18
 * @description
 */
public class UpdateUserServlet extends HttpServlet {

    private IUserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = request.getParameter("userName");
        //源对象
        User user = userService.selectOne(userName);

        //修改值
        String trueName = request.getParameter("trueName");
        user.setTrueName(trueName);
        String sex = request.getParameter("sex");
        user.setSex(Integer.parseInt(sex));
        String age = request.getParameter("age");
        user.setAge(Integer.parseInt(age));
        String phone = request.getParameter("phone");
        user.setPhone(phone);
        String email = request.getParameter("email");
        user.setEmail(email);
        String[] hobbys = request.getParameterValues("hobby");
        user.setHobbys(hobbys);
        String signature = request.getParameter("signature");
        user.setSignature(signature);
        userService.update(user);

        //判断当前修改的用户不是自己
        User sessionUser = (User) request.getSession().getAttribute("user");
        if(sessionUser.getUserName().equals(userName)){
            request.getSession().setAttribute("user",user);
        }

        request.getRequestDispatcher("/final/userinfo").forward(request,response);
    }
}
